(function($){
    jQuery.fn.submitForm = function()
    {
        $('#sb-nav-save-form').unbind('click').bind('click', function(){
            var iframe = $('#fancybox-frame').contents();
            if ($('.ui-tabs-nav', iframe).size() && $('form', iframe).size() > 1)
            {
                var active_tab = $('.ui-tabs-panel', iframe).not('.ui-tabs-hide');
                $('form', active_tab).submit();
            }
            else
                $('form', iframe).submit();
            return false;
        });
    };
    $(function(){
        $.showLoading = function () {
            $.fancybox.showActivity();
        }
        $.hideLoading = function () {
            $.fancybox.hideActivity();
        }
        jQuery.fn.submitForm();
        $('#sb-nav-close-form').click(function(){
            $.fancybox.close();
            $.each($('#sb-nav-close-form, #sb-nav-reload-form, #sb-nav-save-form'), function(){
                $(this).closest('li').css('display', 'none');
            });
            $.each($('#sb-nav-save-position, #sb-nav-save-revision-position, #sb-nav-copy-revision, #sb-nav-properties, #sb-nav-permissions, #sb-nav-versions, #sb-nav-design, #sb-nav-exit-edit, #sb-nav-add, #sb-nav-edit, #sb-nav-header, #sb-nav-footer'), function(){
                $(this).closest('li').css('display', 'block');
            });
            if ($.page_changed)
                $('#sb-nav-save-position').closest('li').css('display', 'block');
            else
                $('#sb-nav-save-position').closest('li').css('display', 'none');
                
            jQuery.fn.submitForm();
            return false;
        });        
        $('#sb-nav-reload-form').click(function(){
            $('#fancybox-frame').attr('src', $('#fancybox-frame').attr('src'));
            jQuery.fn.submitForm();
            return false;
        });
        $('#sb-nav-edit, #sb-nav-exit-edit, #sb-nav-logout').click(function(){
            if ($(this).attr('id') == 'sb-nav-edit')
            {
                if ($('body.is_active_in_live').size())
                {
                    alert('This revision is live and can only be editted while viewing live workflow.');
                    if ($('#sb-nav-exit-edit').closest('li:hidden').size() <= 0)
                    {
                        $('#sb-nav-exit-edit').trigger('click');
                    }
                    return false;
                }
//                confirm('This revision is live. Do you want to create a new revision before editting?');
                //todo create new revision
            }
            else
            {
                if ($.confirm_discard_changed_page() == false)
                    return false;
            }
            $(this).addClass('sb-nav-loading');
            $.get($(this).attr('href'), $.proxy(function(res){
                $(this).removeClass('sb-nav-loading');
                window.location = window.location.href.replace(/\#.*/, '');
            }, this));
            return false;
        });
        $('#sb-nav-properties, #sb-nav-permissions, #sb-nav-versions').click(function(){
            var matchs = $('body').attr('class').match(/page_(\d+)/);
            var page_id = 0;
            if (matchs && matchs.length == 2)
                page_id = parseInt(matchs[1]);
            var url = $.param.querystring($(this).attr('href'), {'id' : page_id});
            //window.location = decodeURIComponent(url);
            $.loadPage(url);
            return false;
        });
        $('#sb-nav-copy-revision').click(function(){
            $.each($('#sb-nav-close-form, #sb-nav-reload-form'), function(){
                $(this).closest('li').css('display', 'block');
            });
            $.each($('#sb-nav-properties, #sb-nav-permissions, #sb-nav-versions, #sb-nav-design, #sb-nav-exit-edit, #sb-nav-add, #sb-nav-edit, #sb-nav-header, #sb-nav-footer'), function(){
                $(this).closest('li').css('display', 'none');
            });

            var matchs = $('body').attr('class').match(/page_(\d+)/);
            var page_id = 0;
            if (matchs && matchs.length == 2)
                page_id = parseInt(matchs[1]);    
            var revision = 0;
            var matchs = $('body').attr('class').match(/\srevision_(\d+)/);
            if (matchs && matchs.length == 2)
                revision = parseInt(matchs[1]);
            var url = $.param.querystring($(this).attr('href'), {'page_id' : page_id, 'revision' : revision});
            $.fancybox({
                'width'             : '97%',
                'height'            : '70%',
                'top'               : 30,
                'margin'            : 0,
                'padding'           : 0,
                'modal'             : true,
                'autoScale'         : false,
                'titleShow'         : false,
                'transitionIn'      : 'none',
                'transitionOut'     : 'none',
                'type'              : 'iframe',
                'href'              : decodeURIComponent(url),
                'onComplete'        : function(){
                    $('html,body').animate({scrollTop : 0}, 100);
                }
            });
            return false;
        });
        $('#sb-nav-add').click(function(){
            var matchs = $('body').attr('class').match(/page_(\d+)/);
            var page_id = 0;
            if (matchs && matchs.length == 2)
                page_id = parseInt(matchs[1]);
            var url = $(this).attr('href');
            //window.location = decodeURIComponent(url);
            $.loadPage(url);
            return false;
        });
        $('#sb-nav-header, #sb-nav-footer, #sb-nav-design').click(function(){
            var matchs = $('body').attr('class').match(/page_(\d+)/);
            var page_id = 0;
            if (matchs && matchs.length == 2)
                page_id = parseInt(matchs[1]);
            
            var matchs = $('body').attr('class').match(/revision_(\d+)/);
            var revision_id = 0;
            if (matchs && matchs.length == 2)
                revision_id = parseInt(matchs[1]);
            var url = $.param.querystring($(this).attr('href'), {'page_id' : page_id, 'revision_id' : revision_id});
            $.loadPage(url);
            return false;
        });
        $.loadPage = function(url)
        {
            $.each($('#sb-nav-close-form, #sb-nav-reload-form, #sb-nav-save-form'), function(){
                $(this).closest('li').css('display', 'block');
            });
            $.each($('#sb-nav-save-position, #sb-nav-save-revision-position, #sb-nav-copy-revision, #sb-nav-properties, #sb-nav-permissions, #sb-nav-versions, #sb-nav-design, #sb-nav-exit-edit, #sb-nav-add, #sb-nav-edit, #sb-nav-header, #sb-nav-footer'), function(){
                $(this).closest('li').css('display', 'none');
            });
            $.fancybox({
                'width'             : '97%',
                'height'            : '70%',
                'top'               : 30,
                'margin'            : 0,
                'padding'           : 0,
                'modal'             : true,
                'autoScale'         : false,
                'titleShow'         : false,
                'transitionIn'      : 'none',
                'transitionOut'     : 'none',
                'type'              : 'iframe',
                'href'              : decodeURIComponent(url),
                'onComplete'        : function(){
                    $('html,body').animate({scrollTop : 0}, 100);
                }
            });
        }

        $.log = null;

        $.save_new_revision = function(text){
            $.fancybox.close();
            $.log = text;
            $.save_position(true);
            $.log = null;
        }

        $('#sb-nav-save-position').click(function(){
            if (confirm('You are about to save your changes in the same revision. Are you sure?'))
            {
                $.save_position(false);
            }
            return false;
        });

        $.page_changed = false;
        $.confirm_discard_changed_page = function(){
            if ($.page_changed)
                return confirm('The page is changed, Discard your changed?');
            return true;
        }
        
        setTimeout(function(){
            if ($('body.is_active_in_live').size())
            {
                if ($('#sb-nav-exit-edit').size())
                {
                    alert('This revision is live and can only be editted while viewing live workflow.');
                    $('#sb-nav-exit-edit').trigger('click');
                }
            }
        }, 500);
    });
})(jQuery);